package com.lazi.june.common.base;

import com.lazi.june.common.exception.CommonException;
import com.lazi.june.common.exception.ExceptionEnum;
import com.lazi.june.common.exception.ExceptionService;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.Serializable;

/**
 * desc:
 *
 * @author huliqian
 * @since 2025/8/21
 */
@Getter
@AllArgsConstructor
public class Result<T> implements Serializable {


    private static final long serialVersionUID = 1L;


    /**
     * 响应码
     */
    private int code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;


    public static <T> Result<T> ok() {
        return new Result<>(ExceptionEnum.SUCCESS.getCode(), ExceptionEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ExceptionEnum.SUCCESS.getCode(), ExceptionEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return new Result<>(ExceptionEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> error(ExceptionService service) {
        return error(service.getCode(), service.getMessage());
    }

    public static CommonException failed(ExceptionService service) {
        return new CommonException(service.getCode(), service.getMessage());
    }

    public static CommonException failed(ExceptionService service, String message) {
        return new CommonException(service.getCode(), message);
    }

    public static CommonException failed(ExceptionService service, Throwable cause) {
        return new CommonException(service.getCode(), service.getMessage(), cause);
    }

    public static CommonException failed(ExceptionService service, String message, Throwable cause) {
        return new CommonException(service.getCode(), message, cause);
    }
}
